package com.xpsoft.oa.workflow.event;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.produce.MaterialAllocation;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.produce.MaterialAllocationService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.DepartmentService;

public class MaterialAllocationFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(MaterialAllocationFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String fromDeptId = (String)pi.getVariable("materialAllocation.fromDept.depId");
		String operatorId = (String)pi.getVariable("materialAllocation.operator.userId");
		Date fromDate = (Date)pi.getVariable("materialAllocation.fromDate");
		String toDeptId = (String)pi.getVariable("materialAllocation.toDept.depId");
		String receiverUserId = (String)pi.getVariable("materialAllocation.receiverUser.userId");
		Date toDate = (Date)pi.getVariable("materialAllocation.toDate");
		String reason = (String)pi.getVariable("materialAllocation.reason");
		String dataList = (String)pi.getVariable("materialAllocation.dataList");
		
		MaterialAllocation materialAllocation = new MaterialAllocation();
		materialAllocation.setFromDate(fromDate);
		materialAllocation.setToDate(toDate);
		materialAllocation.setReason(reason);
		
		AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
		DepartmentService departmentService = (DepartmentService)AppUtil.getBean("departmentService");
		
		if(StringUtils.isNotBlank(fromDeptId)){
			materialAllocation.setFromDept(departmentService.get(Long.valueOf(fromDeptId)));
		}
		if(StringUtils.isNotBlank(operatorId)){
			materialAllocation.setOperator(appUserService.get(Long.valueOf(operatorId)));
		}
		
		if(StringUtils.isNotBlank(receiverUserId)){
			materialAllocation.setReceiverUser(appUserService.get(Long.valueOf(receiverUserId)));
		}
		if(StringUtils.isNotBlank(toDeptId)){
			materialAllocation.setToDept(departmentService.get(Long.valueOf(toDeptId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		materialAllocation.setProcessRunId(processRun.getRunId());
		
		MaterialAllocationService materialAllocationService = (MaterialAllocationService)AppUtil.getBean("materialAllocationService");
		materialAllocationService.saveMaterialAllocation(materialAllocation, dataList);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
